<template>
  <div class="license">
    <p class="detail-tip">
      营业执照<span v-if="showView && !isStatic" @click="toEdit">编辑</span>
    </p>
    <div class="com-container" v-if="showView">
      <div class="show-item">
        <img class="img" v-if="imgUrl" :src="imgUrl" />
        <p class="btn-wrap" v-if="imgUrl && isStatic">
          <span class="edit" @click="toDowload">下载</span>
        </p>
      </div>
    </div>
    <div class="com-form" v-else>
      <div class="postForm">
        <div class="form-item">
          <div class="text" required>上传营业执照:</div>
          <div class="input">
            <input
              ref="business_file"
              type="file"
              @change="uploadFile($event)"
              accept="image/jpeg,image/jpg,image/png"
              class="hidden"
            />
            <p class="btn" @click="upload">上传文件</p>
            <p class="tips">支持 5M 以内 jpeg、jpg、png 格式的文件</p>
          </div>
        </div>
        <div class="img" v-if="imgUrl">
          <img :src="imgUrl" />
        </div>
        <div class="submit">
          <button class="confirm" type="button" @click="submit">保存</button>
          <button class="cancel" type="button" @click="cancel">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { uploadFile } from '@/api/users';
export default {
  props: ['business', 'isStatic'],
  data () {
    return {
      showView: true,
      imgUrl: '',
    }
  },
  watch: {
    business (val) {
      this.imgUrl = val;
    }
  },
  created () {
    this.imgUrl = this.business;
  },
  mounted () {
  },
  methods: {
    uploadFile (e) {
      const file = e.target.files[0];
      if (!file) { return }
      // const imgSize = file.size / 1024 / 1024;
      // const fileType = file.type;
      const reader = new FileReader();
      reader.onloadend = data => {
        // const dataURL = reader.result;
        // console.log(data)
        const formData = new FormData();
        formData.append('file', file);
        uploadFile(formData).then(res => {
          if (res.code === 200) {
            this.imgUrl = res.data.link;
            this.$message.success('上传成功！');
          } else {
            this.$message.error(res.msg)
          }
        })
      }
      // this.url = file;
      reader.readAsDataURL(file);
    },
    upload () {
      this.$refs.business_file.click();
    },
    toEdit () {
      this.showView = false;
    },
    toDowload () {
      window.open(this.imgUrl, '_blank');
    },
    cancel () {
      this.showView = true;
    },
    submit () {
      this.showView = true;
      this.$emit('submit_license', this.imgUrl || '')
      // this.$emit('submit_license', this.$refs.business_file.files[0] || '')
    },
  }
}
</script>
<style lang="scss" scoped>
.license {
  position: relative;
  .img {
    img {
      width: 500px;
    }
  }
  .show-item {
    line-height: 40px;
    padding-left: 30px;
    margin-bottom: 20px;
    .img {
      width: 700px;
      display: block;
      margin: 0 auto;
    }
    & > span {
      margin-right: 30px;
      display: inline-block;
    }
    .name {
      color: #333;
    }

    .btn-wrap {
      float: right;
      margin-right: 20px;
      span {
        margin-left: 20px;
      }
      .edit {
        color: $base-color;
        cursor: pointer;
        &:hover {
          color: $base-color-hover;
        }
      }
      .delete {
        color: $base-color-danger;
        cursor: pointer;
        &:hover {
          color: $base-color-important;
        }
      }
    }
  }
  .com-form {
    padding: 30px 0;
    .postForm {
      width: 500px;
      margin: 0 auto;
      .form-item {
        margin-bottom: 10px;
        line-height: 40px;
        & > div {
          display: inline-block;
        }
        & > .text {
          position: relative;
          padding-left: 20px;
          min-width: 120px;
          color: #24292f;
          vertical-align: top;
          &[required] {
            &::before {
              content: '*';
              position: absolute;
              left: 0px;
              top: 0px;
              color: $base-color-danger;
            }
          }
        }
        .input {
          input {
            line-height: 28px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 0 10px;
            width: 350px;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          .btn {
            background: $base-color;
            color: #fff;
            display: inline-block;
            width: 120px;
            text-align: center;
            border-radius: 6px;
            border: 1px solid #4771dc;
            box-shadow: 0 0 6px #f1f1f1;
            &:hover {
              cursor: pointer;
              box-shadow: 0 0 6px 0 #4771dc;
            }
          }
        }
      }
    }
  }
}
</style>
